package com.customer.crm.system.controller;


import com.customer.crm.system.pojo.SystemFunction;

import com.customer.crm.system.pojo.SystemUser;
import com.customer.crm.system.service.SystemUserService;
import com.customer.crm.system.service.impl.SystemFunctionServiceImpl;
import com.customer.crm.system.service.impl.SystemUserServiceImpl;
import com.customer.crm.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

import java.io.Serializable;
import java.util.List;


@Controller
public class List_Function_Controller  {
    @Resource
    private SystemFunctionServiceImpl functionServiceImpl;

    @Resource

   private SystemUserServiceImpl systemUserService;

    //字典管理system/dict/DictAction_list.action
    @RequestMapping("/system/function/functionAction_list.action")
    public String Function() {
        return "views/system/list_function";
    }

    //查询所有
    @RequestMapping("/tofunction")
    @ResponseBody
    @Transactional
    public ModelMap toFunction() {
        ModelMap map = new ModelMap();
        List<SystemFunction> list = functionServiceImpl.list();
        for (SystemFunction li:list
        ) {
            String createBy = li.getCreateBy();
            if (createBy!=null){
                SystemUser userPojo = systemUserService.getById(createBy);
                String userName = userPojo.getUserName();
                li.setCreateBy(userName);
            }

            String updateBy = li.getUpdateBy();
            if (updateBy!=null){

                SystemUser userNameUpdate = systemUserService.getById(updateBy);
                String username1 = userNameUpdate.getUserName();
                li.setUpdateBy(username1);
            }
        }
        map.addAttribute("data", list);
        return map;
    }

    //找到添加页面
    @RequestMapping("/function_insert")
    public String function_insert() {
        return "views/system/insert_function";
    }

    //保存修改
    @RequestMapping("/Function_insert_list")
    @ResponseBody
    public ResultData function_insert_list(SystemFunction funPojo,ModelMap modelMap) {
        boolean save = functionServiceImpl.save(funPojo);
        modelMap.addAttribute("success", save);
        ResultData resultData = ResultData.buildSuccessResult("保存失败", save);
        return resultData;
    }

    //删除
    @RequestMapping("/FunctionAction_remove")
    @ResponseBody
    public ModelMap FunctionAction_remove(String[] ids) {
        ModelMap modelMap = new ModelMap();
        for (String id : ids
        ) {
            boolean data = functionServiceImpl.removeById(id);
            modelMap.addAttribute("success", data);
            modelMap.addAttribute("message", "删除成功");

        }
        return modelMap;
    }

    //携带数据调到修改页面
    @RequestMapping("/FunctionAction_update")
    public String FunctionAction_update(String id, ModelMap modelMap) {
        SystemFunction byId = functionServiceImpl.getById(id);
        modelMap.addAttribute("funPojo", byId);
        return "views/system/edit_function";
    }

    //保存修改
    @RequestMapping("/Function_saveOrUpdate")
    @ResponseBody
    public ModelMap Function_saveOrUpdate(SystemFunction funPojo) {
        ModelMap modelMap = new ModelMap();
        boolean data = functionServiceImpl.updateById(funPojo);
        System.out.println(data);
        modelMap.addAttribute("success", data);
        return modelMap;
    }
}
